package net.xo68.destiny.core.rocksdb;


import org.apache.commons.lang3.StringUtils;

import java.nio.charset.Charset;


public class StringRocksSerializer implements RocksSerializer<String> {

    private Charset charset=Charset.defaultCharset();

    public Charset getCharset() {
        return charset;
    }

    public void setCharset(Charset charset) {
        this.charset = charset;
    }

    @Override
    public byte[] serialize(String str) throws RocksSerializationException {
        if(StringUtils.isEmpty(str)){
            throw  new RocksSerializationException("要序列化的值不能为空");
        }
        return str.getBytes(charset);
    }

    @Override
    public String deserialize(byte[] bytes) {
        if(bytes==null || bytes.length==0){
            throw  new RocksSerializationException("要反序列化的值不能为空");
        }
        return new String(bytes,charset);
    }


}
